package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {
    @Autowired
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;


    @Autowired
    private IWorkerCertificationAuditService auditService;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                                                                         .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                                                                         .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                                                                         .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                                                                         .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                                                                         .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                                                                         .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                                                                         .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                                                                         .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 提交认证申请
     */
    @Override
    public void WorkerCertificationAudit(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {
        // 获取当前用户id
        Long userId = UserContext.currentUserId();
        // 转换成服务人员认证信息
        WorkerCertification workerCertification = BeanUtil.copyProperties(workerCertificationAuditAddReqDTO, WorkerCertification.class);

        // 设置认证状态
        workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());

        // 设置服务人员id
        workerCertification.setId(userId);
        // 设置认证时间
        workerCertification.setCertificationTime(LocalDateTime.now());
        // 保存

        this.save(workerCertification);
        //服务人员审核

        WorkerCertificationAudit audit = BeanUtil.copyProperties(workerCertification, WorkerCertificationAudit.class,"id");
        //设置服务人员id
        audit.setServeProviderId(userId);
//        audit.setAuditStatus(0);           // 待审核
//        audit.setAuditorId(null);
//        audit.setAuditorName("系统");
//        audit.setAuditTime(null);
//        audit.setCertificationStatus(1);   // 认证进行中
//        audit.setRejectReason(null);

        auditService.save(audit);


    }


    /**
     * 获取驳回原因
     */
    @Override
    public RejectReasonResDTO rejectReason() {
        // 获取当前用户id
        Long userId = UserContext.currentUserId();
        return baseMapper.rejectReason(userId);
    }
}
